package com.ggkt.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * @author ggkt
 * @version V1.0
 * @Description:
 * @since 2021-04-26
 */
@Slf4j
public class DateUtils {
    public static final DateUtils me=new DateUtils();
    public static final String YYYYMMDD="yyyy-MM-dd";
    public static final SimpleDateFormat YYYYMMDD_FMT=new SimpleDateFormat(YYYYMMDD);
    public static final String YYYY_MM_DD="yyyy-MM-dd";
    public static final SimpleDateFormat YYYY_MM_DD_FMT=new SimpleDateFormat(YYYY_MM_DD);
    public static final String YYYY_MM_DD_HH_MM_SS="yyyy-MM-dd HH:mm:ss";
    public static final SimpleDateFormat YYYY_MM_DD_HH_MM_SS_FMT=new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
    public static final String YYYYMMDDHHMMSS="yyyyMMddHHmmss";
    public static final SimpleDateFormat YYYYMMDDHHMMSS_FMT=new SimpleDateFormat(YYYYMMDDHHMMSS);
    public static final String HH_MM_SS="HH:mm:ss";
    public static final SimpleDateFormat HH_MM_SS_FMT=new SimpleDateFormat(HH_MM_SS);
    public static final String YYYYMMDDHHMMSSZ = "yyyy-MM-dd'T'HH:mm:ss.SSS Z";
    public static final SimpleDateFormat YYYY_MM_DD_HH_MM_SS_Z = new SimpleDateFormat(YYYYMMDDHHMMSSZ);

    public static void main(String[] args) {
        System.out.println(DateUtils.getDateStr4Format(new Date(),DateUtils.YYYY_MM_DD_HH_MM_SS_FMT));
    }
    /**
     * 获得当前日期
     * @param sdf 传DateUtils的常量
     * @return
     */
    public static String getToday(SimpleDateFormat sdf){
        return sdf.format(new Date());
    }

    /**
     * 将日期按照指定格式化为字符串
     * @param date
     * @param sdf
     * @return
     */
    public static String getDateStr4Format(Date date,SimpleDateFormat sdf){
        if (null==date){
            return "";
        }
        return sdf.format(date);
    }

    /**
     * 将日期按照指定格式化为日期
     * @param date
     * @param sdf
     * @return
     */
    public static Date getDate4Format(Date date,SimpleDateFormat sdf) {
        if (null == date) {
            return null;
        }
        try {
            return sdf.parse(getDateStr4Format(date, sdf));
        } catch (ParseException e) {
        }
        return null;
    }

    /**
     * 将日期字符串按照指定格式化为日期
     * @param dateStr
     * @param sdf
     * @return
     */
    public static Date getDate4StrFormat(String dateStr,SimpleDateFormat sdf) {
        if (StringUtils.isNotEmpty(dateStr)) {
            try {
                return sdf.parse(dateStr);
            } catch (ParseException e) {
               log.error("转换异常",e);
            }
        }
        return null;
    }

    /**
     * 获得财月 YYYY-MM-DD 00:00:00 YYYY-MM-DD 23:56:59
     * @return
     */
    public static Calendar[] getFiscalMonth(Calendar now){
        Calendar startDate=Calendar.getInstance(),endDate=Calendar.getInstance();
        Integer month=now.get(Calendar.MONTH); //0-11
        Integer year=now.get(Calendar.YEAR);
        if(month==0){//1月26日
            startDate.set(Calendar.YEAR,year-1);
            startDate.set(Calendar.MONTH,11);
            startDate.set(Calendar.DAY_OF_MONTH,26);
        }else{
            startDate.set(Calendar.YEAR,year);
            startDate.set(Calendar.MONTH,month-1);
            startDate.set(Calendar.DAY_OF_MONTH,26);
        }
        startDate.set(Calendar.HOUR,0);
        startDate.set(Calendar.MINUTE,0);
        startDate.set(Calendar.SECOND,0);
        startDate.set(Calendar.MILLISECOND,0);

        endDate.set(Calendar.YEAR,year);
        endDate.set(Calendar.MONTH,month);
        endDate.set(Calendar.DAY_OF_MONTH,25);

        endDate.set(Calendar.HOUR,23);
        endDate.set(Calendar.MINUTE,59);
        endDate.set(Calendar.SECOND,59);
        endDate.set(Calendar.MILLISECOND,0);
        return new Calendar[]{startDate,endDate};
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1,Date date2)
    {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        return days;
    }
    /**
     * 获取指定年月的第一天，如果参数格式错误，则重置成当前月份的第一天
     * @param year
     * @param month
     * @return
     * @author sixmonth
     * @date 2019年6月5日
     *
     */
    public static String getMonthFirstDay(int year, int month) {
        Calendar cal = Calendar.getInstance();
        if(month>0&&month<=12&&year>0) {
            //设置年份
            cal.set(Calendar.YEAR, year);
            //设置月份
            cal.set(Calendar.MONTH, month-1);
            //设置日历中月份的最小天数
            cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DATE));
            //格式化日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(cal.getTime());
        }else {
            //设置当前年月日
            cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH));
            //将日时间设置成本月第一天
            cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DAY_OF_MONTH));
            //格式化日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(cal.getTime());
        }
    }

    /**
     * 获取指定年月的最后一天，如果参数格式错误，则重置成当前月份的最后一天
     * @param year
     * @param month
     * @return
     * @author sixmonth
     * @date 2019年6月5日
     *
     */
    public static String getMonthLastDay(int year, int month) {
        Calendar cal = Calendar.getInstance();
        if(month>0&&month<=12&&year>0) {
            //设置年份
            cal.set(Calendar.YEAR, year);
            //设置月份
            cal.set(Calendar.MONTH, month-1);
            //设置日历中月份的最大天数
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
            //格式化日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(cal.getTime());
        }else {
            //设置当前年月日
            cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH));
            //将日时间设置成本月最后一天
            cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            //格式化日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(cal.getTime());
        }
    }
    public static String getDate(int year, int month) {
        Calendar cal = Calendar.getInstance();
        if(month>0&&month<=12&&year>0) {
            //设置年份
            cal.set(Calendar.YEAR, year);
            //设置月份
            cal.set(Calendar.MONTH, month-1);
            cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.DAY_OF_MONTH));
        }
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(cal.getTime());
    }

}
